﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace CMS.DataAccess
{
    public class CondCollection: IEnumerable<WhereCond>
    {
        List<WhereCond> whereConds = new List<WhereCond>();
        public List<OrderByField> OrderBy { get; private set; } = new List<OrderByField>();

        public static CondCollection True()
        {
            var v = new CondCollection();
            return v;
        }
        public void AddWhereCond(WhereCond cond)
        {
            this.whereConds.Add(cond);
        }
        public void AddWhereCond(CondCollection conds)
        {
            foreach(var cond in conds)
                this.whereConds.Add(cond);
        }

        public CondCollection Eq(string key, object? value)
        {
            if (value != null)
                whereConds.Add(new WhereCond(key, OpEnum.Eq, value));
            else
                whereConds.Add(new WhereCond(key, OpEnum.IsNull));
            return this;
        }
        public CondCollection EqIgnoreCase(string key, object? value)
        {
            if (value != null)
                whereConds.Add(new WhereCond(key, OpEnum.EqIgnoreCase, value.ToString()!.ToUpper()));
            else
                whereConds.Add(new WhereCond(key, OpEnum.IsNull));
            return this;
        }
        public CondCollection NoEq(string key, object? value)
        {
            if (value != null)
                whereConds.Add(new WhereCond(key, OpEnum.NotEq, value));
            else
                whereConds.Add(new WhereCond(key, OpEnum.IsNotNull));
            return this;
        }
        public CondCollection Lk(string key, object? value)
        {
            if (value != null)
                whereConds.Add(new WhereCond(key, OpEnum.Lk, value));
            else
                whereConds.Add(new WhereCond(key, OpEnum.IsNull));
            return this;
        }
        public CondCollection IsNull(string key)
        {
            whereConds.Add(new WhereCond(key, OpEnum.IsNull));
            return this;
        }

        public CondCollection IsNotNull(string key)
        {
            whereConds.Add(new WhereCond(key, OpEnum.IsNotNull));
            return this;
        }
        public CondCollection Ge(string key, object value)
        {
            whereConds.Add(new WhereCond(key, OpEnum.Ge, value));
            return this;
        }
        public CondCollection Gt(string key, object value)
        {
            whereConds.Add(new WhereCond(key, OpEnum.Gt, value));
            return this;
        }
        public CondCollection Le(string key, object value)
        {
            whereConds.Add(new WhereCond(key, OpEnum.Le, value));
            return this;
        }
        public CondCollection Lt(string key, object value)
        {
            whereConds.Add(new WhereCond(key, OpEnum.Lt, value));
            return this;
        }

        public IEnumerator<WhereCond> GetEnumerator()
        {
            return whereConds.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
